﻿using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;

public class DialogPanel : MonoBehaviour
{
    public GameObject _dialogPrefab;
    private Transform _content;
    private int _index = 0;
    private Action _action;

    public void ShowDialog(Action action)
    {
        _content = transform.Find("Content");
        _action = action;
        StartCoroutine(InitDialog());
    }

    IEnumerator InitDialog()
    {
        yield return new WaitForSeconds(2f);

        if (_index >= Constants.Dialog[GameManager.Instance.MissionIndex].Count)
        {
            _content.GetComponent<RectTransform>().DOAnchorPosX(-1000, 1).SetDelay(1).OnComplete(() =>
            {
                gameObject.SetActive(false);
                _action?.Invoke();
            });
            yield break;
        }

        var go = Instantiate(_dialogPrefab);
        go.transform.SetParent(_content);
        go.GetComponent<DialogBubble>()
            .Set(Constants.Dialog[GameManager.Instance.MissionIndex][_index], Constants.Characters[GameManager.Instance.MissionIndex],
                () => StartCoroutine(InitDialog()));
        _index++;
    }
}